﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using EHTSaleManager.Code.Entity;
using EHTSaleManager.Code.DAL;
using System.Data.SqlClient;
using System.Data;

namespace EHTSaleManager.Code.BLL
{
    public class CategoryProvider
    {
        public static void IUCategory(Category theCategory, bool IsAdd)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_IUCategorys";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@CategoryID", theCategory.CategoryID));
            command.Parameters.Add(new SqlParameter("@CategoryName", theCategory.CategoryName));
            command.Parameters.Add(new SqlParameter("@Description", theCategory.Description));
            command.Parameters.Add(new SqlParameter("@PictureUrl", theCategory.PictureUrl));
            command.Parameters.Add(new SqlParameter("@ParentID", theCategory.ParentID));
            command.Parameters.Add(new SqlParameter("@DeletedFlag", theCategory.DeletedFlag));
            command.Parameters.Add(new SqlParameter("@Status", theCategory.Status));
            command.Parameters.Add(new SqlParameter("@Ordering", theCategory.Ordering));
            command.Parameters.Add(new SqlParameter("@IsAdd", IsAdd));

            DataProvider.ExecuteNoneQuery(command);
        }
        public static void DeleteCategory(Int32 CategoryID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_DeleteCategoryByCategoryID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@CategoryID", CategoryID));

            DataProvider.ExecuteNoneQuery(command);
        }

        public static DataSet GetCategorysForBackEnd()
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetAllCategorysForBackEnd";
            command.CommandType = CommandType.StoredProcedure;

            return DataProvider.ExecuteDataSet(command);
        }

        public static DataSet GetCategorysForFrontEnd()
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetAllCategorysForFrontEnd";
            command.CommandType = CommandType.StoredProcedure;

            return DataProvider.ExecuteDataSet(command);
        }

        public static DataSet GetAllCategorys(Int32 pageIndex, Int32 pageSize, out Int32 totalRecord)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetAllCategorys";
            command.CommandType = CommandType.StoredProcedure;

            command.Parameters.Add(new SqlParameter("@PageIndex", pageIndex));
            command.Parameters.Add(new SqlParameter("@PageSize", pageSize));
            DataSet ds = DataProvider.ExecuteDataSet(command, out totalRecord);
            return ds;
        }

        public static Category GetCategoryByCategoryID(Int32 CategoryID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetCategoryByCategoryID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@CategoryID", CategoryID));

            DataSet ds = DataProvider.ExecuteDataSet(command);
            Category theCategory = null;

            if (ds.Tables[0].Rows.Count > 0)
            {
                theCategory = new Category();
                theCategory.DeletedFlag = Boolean.Parse(ds.Tables[0].Rows[0]["DeletedFlag"].ToString());
                theCategory.Description = ds.Tables[0].Rows[0]["Description"].ToString();
                theCategory.CategoryName = ds.Tables[0].Rows[0]["CategoryName"].ToString();               
                theCategory.CategoryID = Int32.Parse(ds.Tables[0].Rows[0]["CategoryID"].ToString());
                theCategory.PictureUrl = ds.Tables[0].Rows[0]["PictureUrl"].ToString();
                theCategory.ParentID = Int32.Parse(ds.Tables[0].Rows[0]["ParentID"].ToString());
                theCategory.Ordering = Int32.Parse(ds.Tables[0].Rows[0]["Ordering"].ToString());
            }

            return theCategory;
        }

        public static DataSet GetTopCategorys()
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetTopCategory";
            command.CommandType = CommandType.StoredProcedure;

            return DataProvider.ExecuteDataSet(command);
        }

        public static DataSet GetCategorysByParentID(int parentID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetCategorysByParentID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@ParentID", parentID));

            DataSet ds = DataProvider.ExecuteDataSet(command);

            return ds;
        }



        public static DataSet GetCategorysByParentID(int parentID, Int32 pageIndex, Int32 pageSize, out Int32 totalRecord)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetCategorysByParentIDWithPaging";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@CategoryID", parentID));
            command.Parameters.Add(new SqlParameter("@PageIndex", pageIndex));
            command.Parameters.Add(new SqlParameter("@PageSize", pageSize));
            DataSet ds = DataProvider.ExecuteDataSet(command, out totalRecord);
            return ds;
        }


        // search category by category name

        public static DataSet GetCategorysByCategoryName(string CategoryName, Int32 pageIndex, Int32 pageSize, out Int32 totalRecord)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetCategorysByCategoryName";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@CategoryName", CategoryName));

            command.Parameters.Add(new SqlParameter("@PageIndex", pageIndex));
            command.Parameters.Add(new SqlParameter("@PageSize", pageSize));
            DataSet ds = DataProvider.ExecuteDataSet(command, out totalRecord);
            return ds;
        }
       

    }
}